// pages/activityCenter/detail.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent',
      isShare: 0
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,

    btnText: '立即报名',
    mySf: '',
    openid: '',
    shareTitle: '',
    shareImg: '',
    tipsContent: '',
    is_register: 0,
    id: 0,
    mid: 0,
    info: [],
    bm_status: 0,
    isType: 1, //1活动详情 2资讯详情 3.社群 4.生日详情
    is_bm: 1, //是否到了报名时间了
    is_cancel_bm: 0, //是否取消了2次本次活动的报名
    isOneShow: 1, //是否是第一次触发show
    ylImgList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    var id = options.id;
    var isType = options.type;
    var is_share = options.is_share;
    var sceneInfo = options.scene;
    if (!sceneInfo) {
      //不是扫码进入
      if (id == '' || id == '0') {
        app.showTip('抱歉,未获取到相关的id信息哦');
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          });
        }, 2000);
        return;
      }
      this.setData({
        isType: isType,
        id: id
      });
    } else {
      //扫码进入
      this.setData({
        isType: 1,
        id: sceneInfo
      });
      is_share = 1;
      id=sceneInfo;
    }


    //判断返回的跳转路径
    if (is_share == 1) {
      that.setData({
        ['defaultData.isShare']: 1
      });
    }

    //获取缓存中的openid
    var openid = wx.getStorageSync('ly24_openid');

    if (openid == '') {
      //缓存中没有openid
      //获取所需页面信息
      wx.login({
        success: function (res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: that.data.baseUrl + 'getOpenid.php',
              method: 'POST',
              data: {
                'code': res.code
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function (res) {
                //缓存微信openid
                wx.setStorageSync('ly24_openid', res.data.openid);
                that.setData({
                  openid: res.data.openid
                });

                //获取本页数据
                that.getActiveDetailPageInfo(res.data.openid, id, isType);
              },
              fail: function (res) {}
            });
          }
        }
      });
    } else {
      that.setData({
        openid: openid
      });

      //获取本页数据
      that.getActiveDetailPageInfo(openid, id, isType);
    }
  },

  //获取本页数据
  getActiveDetailPageInfo(openid, id, isType) {
    var that = this;
    wx.showLoading({
      title: '数据加载中...'
    })
    wx.request({
      url: that.data.baseUrl + 'getActiveDetailPageInfo.php',
      method: 'POST',
      data: {
        'openid': openid,
        'id': id,
        'isType': isType
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          //判断报名状态
          if (res.data.bm_status == 2) {
            that.setData({
              btnText: '报名已满'
            });
          }

          if (res.data.bm_status == 3) {
            that.setData({
              btnText: '报名结束'
            });
          }

          var html = that.formatRichText(res.data.info['content']);
          //重新赋值后台请求数据
          that.setData({
            is_register: res.data.is_register,
            mySf: res.data.mySf,
            info: res.data.info,
            html: html,
            bm_status: res.data.bm_status,
            is_bm: res.data.is_bm,
            tipsContent: res.data.tipsContent,
            is_cancel_bm: res.data.is_cancel_bm,
            ylImgList: res.data.ylImgList
          });

          //判断是否是活动
          if (isType == '1') {
            that.setData({
              shareTitle: res.data.info.title,
              shareImg: that.data.baseUrl + 'admin/' + res.data.info.img_path
            })
          }
        } else {
          app.showTip(res.data.msg);
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            });
          }, 2000);
        }
      },
      fail: function () {
        wx.hideLoading();
        app.showTip('抱歉,服务器繁忙');
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          });
        }, 2000);
      }
    });

  },

  //跳转下一页面
  appointTap() {
    var that = this;
    if (that.data.is_register == '0') {
      wx.showModal({
        title: '温馨提示',
        content: that.data.tipsContent,
        complete: (res) => {
          if (res.confirm) {
            //跳转注册界面
            wx.setStorageSync('loginHeaderUrl', '/pages/activityCenter/detail?id=' + that.data.id + "&isType=1");
            wx.setStorageSync('loginHeaderType', 'redirectTo')
            if (res.confirm) {
              //跳转注册界面
              wx.redirectTo({
                url: '/pages/login/select-login/select-login'
              })
            }
          }
        }
      })
      return;
    } else {

      //判断是否可以报名
      if (that.data.is_bm == '0') {
        app.showTip('抱歉,该活动未到报名时间哦');
        return;
      }

      //判断此时的活动报名状态
      if (that.data.bm_status == 1 && that.data.info.activity_type == 3) {
        //是问卷
        app.showTip('您已经提交过此问卷了哦');
        return;
      } else if (that.data.bm_status == 1 && that.data.info.activity_type != 3) {
        //不是问卷报名
        app.showTip('抱歉,你已达报名人数上限了哦,暂无法报名');
        return;
      } else if (that.data.bm_status == 2) {
        app.showTip('抱歉,活动报名人数已满');
        return;
      } else if (that.data.bm_status == 3) {
        app.showTip('抱歉,本场活动报名已结束');
        return;
      }

      //判断此活动是否今日已经取消报名2次了
      if (that.data.is_cancel_bm == '0') {
        app.showTip('抱歉,此活动今日您已取消预约2次,今日暂无法报名了哦');
        return;
      }

      //活动 逻辑判断 报名身份
      if (that.data.info['is_bm'] == 1) {
        //判断是否是单独问卷
        if (that.data.info.activity_type == 3) {
          //清空到问卷页的缓存
          wx.setStorageSync('orderArr', {});
          //缓存订单信息
          wx.setStorageSync('orderArr', {
            id: that.data.id,
            tid: -1,
            sid: -1,
            people_number: 0,
            team: '',
            room_number: '',
            remark: ''
          });
          //跳转问卷
          wx.navigateTo({
            url: '/pages/activityCenter/question'
          });
        } else if (that.data.info.activity_type == 2) {
          //跳转交房
          //判断是否是业主或者家人
          if (that.data.mySf == '业主' || that.data.mySf == '家人') {
            //是业主或者家人
            wx.navigateTo({
              url: '/pages/activityCenter/jf2?id=' + that.data.id
            });
          } else {
            //是游客
            app.showTip('抱歉,此活动仅限' + that.data.info.partake_sf + '参与哦');
            return;
            // wx.navigateTo({
            //   url: '/pages/activityCenter/jf1?id=' + that.data.id
            // });
          }
        } else {
          //普通活动
          wx.navigateTo({
            url: '/pages/activityCenter/bm?id=' + that.data.id
          });
        }
      } else {
        //没有报名权限
        app.showTip('抱歉,本活动仅限' + that.data.info.partake_sf + "参与哦");
        return;
      }
    }
  },

  //预览图片
  previewImage(e) {
    let that = this,
      imgsrc = that.data.ylImgList[0],
      arr = that.data.ylImgList
    wx.previewImage({
      current: imgsrc,
      urls: arr
    })
  },

  /**
   * 处理富文本里的图片宽度自适应
   * 1.去掉img标签里的style、width、height属性
   * 2.img标签添加style属性：max-width:100%;height:auto
   * 3.修改所有style里的width属性为max-width:100%
   * 4.去掉<br/>标签
   * @param html
   * @returns {void|string|*}
   */
  formatRichText(html) {
    let newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {
      match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
      match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
      match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
      return match;
    });
    newContent = newContent.replace(/style="[^"]+"/gi, function (match, capture) {
      match = match.replace(/width:[^;]+;/gi, 'width:100%;').replace(/width:[^;]+;/gi, 'width:100%;');
      return match;
    });
    newContent = newContent.replace(/<br[^>]*\/>/gi, '');
    newContent = newContent.replace(/\<img/gi, '<img style="width:100%;height:auto;display:block;margin:10px 0;"');
    return newContent;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;

    if (that.data.isType == 1 && that.data.isOneShow == 0) {
      //活动更新报名状态
      that.getActiveDetailPageInfo(that.data.openid, that.data.id, that.data.isType);
    } else {
      that.setData({
        isOneShow: 0
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;

    return {
      title: that.data.shareTitle,
      path: '/pages/activityCenter/detail?type=' + that.data.isType + "&id=" + that.data.id + '&is_share=1',
      imageUrl: that.data.shareImg,
      success: function (res) {

      },
      fail: function (res) {

      }
    }
  }
})